package mx.com.matersys.vacs.daos;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import mx.com.matersys.vacs.domain.Usuarios;

import org.springframework.transaction.annotation.Transactional;

privileged aspect UserDao {
	
	declare @type: Usuarios: @Entity;
    
    @PersistenceContext
    transient EntityManager Usuarios.entityManager;
    
    @Transactional
    public void Usuarios.persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }
    
    @Transactional
    public void Usuarios.remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Usuarios attached = Usuarios.findUser(this.getUsername());
            this.entityManager.remove(attached);
        }
    }
    
    @Transactional
    public void Usuarios.flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }
    
    @Transactional
    public void Usuarios.clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }
    
    @Transactional
    public Usuarios Usuarios.merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Usuarios merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }
    
    public static final EntityManager Usuarios.entityManager() {
        EntityManager em = new Usuarios().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }
    
    /*
    
    public static long Usuarios.countUsers() {
        return entityManager().createQuery("SELECT COUNT(o) FROM User o", Long.class).getSingleResult();
    }
    
    public static List<User> User.findAllUsers() {
        return entityManager().createQuery("SELECT o FROM User o", User.class).getResultList();
    }
    
    public static List<User> User.findUserEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM User o", User.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }
    */
    
    public static Usuarios Usuarios.findUser(String username) {
		if (username == null)
			return null;
		
		return entityManager().find(Usuarios.class, username);
    }
    
        
}
